package com.xhwl.centiotdata.suppliersystem.haikangyunmou;

import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.HaiKangYunMouConstant;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouConfig;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouJson;
import com.xhwl.centiotdata.pojo.dto.haikangyunmou.HaiKangYunMouProject;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/14/ 09:00
 */
@Slf4j
@Service
public class UserHaiKangYunMouHandler {

    @Autowired
    private HaiKangYunMouUtils haiKangYunMouUtils;

    public String addPerson(UserDTO userDTO, PedestrianIntegrationConfigDTO configDTO,Integer accountId) {
        Map<String, Object> params = new HashMap<>();
        params.put(HaiKangYunMouConstant.PERSON_NAME, userDTO.getName());
        params.put(HaiKangYunMouConstant.MOBILE, userDTO.getPhone());
        params.put("unionId","");
        // credentialType:3为其他证件类型  ,credentialNumber:证件号，这里暂时设为手机号
        params.put("credentialType",3);
        params.put("credentialNumber",userDTO.getPhone());
        // 图片要求：大小：200k以内；格式：jpg
        params.put(HaiKangYunMouConstant.FACE_URL, userDTO.getFaceUrl());
        //如果获取的海康默认楼栋不为空 添加到住户中
        List<HaiKangYunMouProject> yunMouBuildings = haiKangYunMouUtils.getDefaultYunMouConfigByProjectId(configDTO.getProjectId(),accountId);
        if (!CollectionUtils.isEmpty(yunMouBuildings)){
            List<Map<String,Object>> personCommunityRels = new ArrayList();
            yunMouBuildings.forEach(yunMouBuilding -> {
                if(yunMouBuilding!=null){
                    Map<String, Object> building = new HashMap<>();
                    building.put("communityId", yunMouBuilding.getCommunityId());
                    building.put("buildingId", yunMouBuilding.getBuildingId());
                    building.put("unitId", yunMouBuilding.getUnitId());
                    building.put("roomId", yunMouBuilding.getRoomId());
                    building.put("identityType", HaiKangYunMouConstant.DEFAULT_IDENTITY_TYPE);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String dateNowStr = sdf.format(new Date());
                    building.put("checkInDate", dateNowStr);
                    personCommunityRels.add(building);
                }
            });
            params.put("personCommunityRels", personCommunityRels);
        }
        HaiKangYunMouConfig haiKangYunMouConfig = new HaiKangYunMouConfig();
        BeanUtils.copyProperties(configDTO, haiKangYunMouConfig);
        HaiKangYunMouJson resp = null;
        try{
            resp = haiKangYunMouUtils.doReq(HaiKangYunMouConstant.PERSON_ADD, params,
                    Method.POST, haiKangYunMouConfig, null);
        }catch (Exception e){
            log.error("调用海康接口异常",e);
        }
        JSONObject jsonObject = (JSONObject) resp.getData();
        return jsonObject.getString(HaiKangYunMouConstant.PERSON_ID);
    }

    public void updatePerson(UserDTO userDTO, PedestrianIntegrationConfigDTO configDTO,String personId){
        Map<String, Object> params = new HashMap<>();
        log.info("personId{}",personId);
        params.put(HaiKangYunMouConstant.PERSON_ID,personId);
        params.put(HaiKangYunMouConstant.PERSON_NAME, userDTO.getName());
        params.put(HaiKangYunMouConstant.MOBILE, userDTO.getPhone());
        params.put(HaiKangYunMouConstant.FACE_URL, userDTO.getFaceUrl());
        params.put(HaiKangYunMouConstant.CREDENTIAL_TYPE,HaiKangYunMouConstant.DEFAULT_IDENTITY_TYPE);
        if (StringUtils.isNotEmpty(userDTO.getPhone())){
            params.put(HaiKangYunMouConstant.CREDENTIAL_NUMBER,userDTO.getPhone());
        }else {
            params.put(HaiKangYunMouConstant.CREDENTIAL_NUMBER,personId);
        }
        HaiKangYunMouConfig haiKangYunMouConfig = new HaiKangYunMouConfig();
        BeanUtils.copyProperties(configDTO, haiKangYunMouConfig);
        try{
            haiKangYunMouUtils.doReq(HaiKangYunMouConstant.PERSON_UPDATE, params,Method.POST, haiKangYunMouConfig, null);
        }catch (Exception e){
            log.error("调用海康接口异常",e);
        }
    }

    public void deletePerson(String personId, PedestrianIntegrationConfigDTO configDTO){
        Map<String, Object> params = new HashMap<>();
        params.put(HaiKangYunMouConstant.PERSON_ID,personId);
        HaiKangYunMouConfig haiKangYunMouConfig = new HaiKangYunMouConfig();
        BeanUtils.copyProperties(configDTO, haiKangYunMouConfig);
        try{
            haiKangYunMouUtils.doReq(HaiKangYunMouConstant.PERSON_DELETE, params,Method.DELETE, haiKangYunMouConfig, null);
        }catch (Exception e){
            log.error("调用海康接口异常",e);
        }
    }

    public void addCommunityRelation(String personId, PedestrianIntegrationConfigDTO configDTO,Integer accountId) {
        Map<String, Object> params = new HashMap<>();
        List<String>  communityIds = haiKangYunMouUtils.getCommunityId(configDTO.getProjectId(),accountId);
        if (!CollectionUtils.isEmpty(communityIds)){
            communityIds.forEach(communityId -> {
                params.put(HaiKangYunMouConstant.PERSON_ID, personId);
                params.put(HaiKangYunMouConstant.COMMUNITY_ID, communityId);
                HaiKangYunMouConfig haiKangYunMouConfig = new HaiKangYunMouConfig();
                BeanUtils.copyProperties(configDTO, haiKangYunMouConfig);
                haiKangYunMouUtils.doReq(HaiKangYunMouConstant.PERSON_COMMUNITY_ADD, params,
                        Method.POST, haiKangYunMouConfig, null);
            });
        }
    }

}
